package com.mobisys.live.controller.pc_web;

import com.alibaba.fastjson.JSONObject;
import com.mobisys.live.dao.model.Live;
import com.mobisys.live.response.Response;
import com.mobisys.live.service.LiveService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.mobisys.live.config.ResponseCode.*;

@RestController
public class LiveWebController {

    @Autowired
    private LiveService liveService;
    private Logger logger = LogManager.getLogger(LiveWebController.class.getName());
    /*
    * 获取所有点播视频
    *
    * */
    @RequestMapping(value = "/home/getLiveList",method = RequestMethod.GET)
    public Response getVideoList(String type, String currPage){

        Response response=new Response();
        List<Live> list = new ArrayList<>();
        JSONObject listobject = new JSONObject();
        int cur = Integer.valueOf(currPage);
        int pageSize = 2;
        //LIVE获取
        list = liveService.findAll(cur,pageSize,type);

        if(!(list.isEmpty())){

            response.setRetureValue(SUCCESS);
            response.setRetureMsg(MSG_SUCCESS);
            listobject.put("listobect",list);
            response.setRetureData(listobject);
            return response;
        }
        response.setRetureValue(LIVE_TYPE_FINDALL_ERROR);
        response.setRetureMsg(MSG_LIVE_TYPE_FINDALL_ERROR);
        logger.debug("直播查找为空");
        listobject.put("listobect",list);
        response.setRetureData(listobject);
        return response;


    }
    /*
    * 得到某一条直播的相关信息
    * */
    @RequestMapping(value = "/live/play",method = RequestMethod.GET)
    public Response getLive(String id){
        Response response=new Response();
        JSONObject liveinfo = new JSONObject();
        Live live = liveService.selectByPrimaryKey(id);
        if(live == null){
            logger.debug("直播查找失败");
            response.setRetureValue(LIVE_TYPE_FINDALL_ERROR);
            response.setRetureMsg(MSG_LIVE_TYPE_FINDALL_ERROR);
        }else {
            response.setRetureValue(SUCCESS);
            response.setRetureMsg(MSG_SUCCESS);
        }
        liveinfo.put("liveinfo",live);
        response.setRetureData(liveinfo);

        return response;
    }
    }


